home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / clcell.jav < prev    next >
Encoding:
Text File  |  1995-10-14  |  4.8 KB  |  245 lines

  1. /*
  2.   File: CLCell.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.   13Oct95  dl                 Changed protection statuses
  12.  
  13. */
  14.   
  15. package collections;
  16.  
  17. import java.util.Enumeration;
  18. import java.util.NoSuchElementException;
  19.  
  20. /**
  21.  *
  22.  *
  23.  * CLCells are cells that are always arranged in circular lists
  24.  * They are pure implementation tools
  25.  * @author Doug Lea
  26.  * @version 0.93
  27.  *
  28.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  29. **/
  30.  
  31. public class CLCell extends Cell {
  32.  
  33. // instance variables
  34.  
  35.   private CLCell next_;
  36.   private CLCell prev_;
  37.  
  38. // constructors
  39.  
  40. /**
  41.  * Make a cell with contents v, previous cell p, next cell n
  42. **/
  43.  
  44.   public CLCell(Object v, CLCell p, CLCell n)  { 
  45.     super(v); prev_ = p; next_ = n;
  46.   }
  47.  
  48. /**
  49.  * Make a singular cell
  50. **/
  51.  
  52.   public CLCell(Object v) { super(v); prev_ = this; next_ = this; }
  53.  
  54. /**
  55.  * Make a singular cell with null contents
  56. **/
  57.  
  58.   public CLCell()         { super(null); prev_ = this; next_ = this; }
  59.  
  60. /**
  61.  * return next cell
  62. **/
  63.  
  64.   public final CLCell next()            { return next_; }
  65.  
  66. /**
  67.  * Set next cell. You probably don't want to call this
  68. **/
  69.  
  70.   public final void         next(CLCell n) { next_ = n; }
  71.  
  72.  
  73. /**
  74.  * return previous cell
  75. **/
  76.   public final CLCell prev()            { return prev_; }
  77.  
  78. /**
  79.  * Set previous cell. You probably don't want to call this
  80. **/
  81.   public final void         prev(CLCell n) { prev_ = n; }
  82.  
  83.  
  84. /**
  85.  * Return true if current cell is the only one on the list
  86. **/
  87.  
  88.   public boolean isSingleton() { return next_ == this; }
  89.  
  90.   public final void linkNext(CLCell p) { 
  91.     if (p != null) {
  92.       next_.prev_ = p;
  93.       p.next_ = next_;
  94.       p.prev_ = this;
  95.       next_ = p;
  96.     }
  97.   }
  98.  
  99. /**
  100.  * Make a cell holding v and link it immediately after current cell
  101. **/
  102.  
  103.   public final void addNext(Object v) { 
  104.     CLCell p = new CLCell(v, this, next_);
  105.     next_.prev_ = p;
  106.     next_ = p;
  107.   }
  108.  
  109. /**
  110.  * make a node holding v, link it before the current cell, and return it
  111. **/
  112.  
  113.   public final CLCell addPrev(Object v) { 
  114.     CLCell p = prev_;
  115.     CLCell c = new CLCell(v, p, this);
  116.     p.next_ = c;
  117.     prev_ = c;
  118.     return c;
  119.   }
  120.  
  121. /**
  122.  * link p before current cell
  123. **/
  124.  
  125.   public final void linkPrev(CLCell p) { 
  126.     if (p != null) {
  127.       prev_.next_ = p;
  128.       p.prev_ = prev_;
  129.       p.next_ = this;
  130.       prev_ = p;
  131.     }
  132.   }
  133.  
  134. /**
  135.  * return the number of cells in the list
  136. **/
  137.  
  138.   public final int length() {
  139.     int c = 0;
  140.     CLCell p = this; 
  141.     do { 
  142.       ++c;
  143.       p = p.next(); 
  144.     } while (p != this); 
  145.     return c;
  146.   }
  147.  
  148. /**
  149.  * return the first cell holding element found in a circular traversal starting
  150.  * at current cell, or null if no such
  151. **/
  152.  
  153.   public final CLCell find(Object element) {
  154.     CLCell p = this; 
  155.     do { 
  156.       if (p.element().equals(element)) return p;
  157.       p = p.next(); 
  158.     } while (p != this); 
  159.     return null;
  160.   }
  161.  
  162. /**
  163.  * return the number of cells holding element found in a circular
  164.  * traversal
  165. **/
  166.  
  167.   public final int count(Object element) {
  168.     int c = 0;
  169.     CLCell p = this; 
  170.     do { 
  171.       if (p.element().equals(element)) ++c;
  172.       p = p.next(); 
  173.     } while (p != this); 
  174.     return c;
  175.   }
  176.  
  177. /**
  178.  * return the nth cell traversed from here. It may wrap around.
  179. **/
  180.  
  181.   public final CLCell nth(int n) {
  182.     CLCell p = this; 
  183.     for (int i = 0; i < n; ++i) p = p.next_;
  184.     return p;
  185.   }
  186.  
  187.  
  188. /** 
  189.  * Unlink the next cell.
  190.  * This has no effect on the list if isSingleton()
  191. **/
  192.  
  193.   public final void unlinkNext()       { 
  194.     CLCell nn = next_.next_;
  195.     nn.prev_ = this;
  196.     next_ = nn;
  197.   }
  198.  
  199. /** 
  200.  * Unlink the previous cell.
  201.  * This has no effect on the list if isSingleton()
  202. **/
  203.  
  204.   public final void unlinkPrev() { 
  205.     CLCell pp = prev_.prev_;
  206.     pp.next_ = this;
  207.     prev_ = pp;
  208.   }
  209.  
  210.  
  211. /**
  212.  * Unlink self from list it is in.
  213.  * Causes it to be a singleton
  214. **/
  215.  
  216.   public final void unlink() { 
  217.     CLCell p = prev_;
  218.     CLCell n = next_;
  219.     p.next_ = n;
  220.     n.prev_ = p;
  221.     prev_ = this;
  222.     next_ = this;
  223.   }
  224.  
  225. /**
  226.  * Make a copy of the list and return new head. 
  227. **/
  228.  
  229.   public CLCell copyList() {
  230.     CLCell hd = this;
  231.     
  232.     CLCell newlist = new CLCell(hd.element(), null, null);
  233.     CLCell current = newlist;
  234.  
  235.     for (CLCell p = next_; p != hd; p = p.next_) {
  236.       current.next_ = new CLCell(p.element(), current, null);
  237.       current = current.next_;
  238.     }
  239.     newlist.prev_ = current;
  240.     current.next_ = newlist;
  241.     return newlist;
  242.   }
  243. }
  244.  
  245.